// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

/// 云函数入口函数
exports.main = async (event, context) => {
  if (event.action == 'add') {
    return await cloud.database().collection('appoint').add({
      data:{
        patientInfo:event.patientInfo,
        doctorInfo:event.doctorInfo,
        user:event.user,
        appointDay:event.appointDay,
        beizhu:event.beizhu,
        isOver:event.isOver,
        isPassed:event.isPassed,
        flowId:event.flowId,
        number:event.number,
        patientAdmin:event.patientAdmin,
        doctorId:event.doctorId
      }
    }).then(res => {
      return res
    }).catch(res => {
      return res
    })
  } 
  else if(event.action=="findOne"){
    return await cloud.database().collection('appoint').where({
      doctorId:event.doctorId,
      patientAdmin:event.patientAdmin,
      appointDay:event.appointDay,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findAll"){
    return await cloud.database().collection('appoint').get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findPatient"){
    return await cloud.database().collection('appoint').where({
      patientAdmin:event.patientAdmin,
      appointDay:event.appointDay,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findPatientNoDay"){
    return await cloud.database().collection('appoint').where({
      patientAdmin:event.patientAdmin,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findDoctorNoDay"){
    return await cloud.database().collection('appoint').where({
      doctorId:event.doctorId,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findPatientShow"){
    return await cloud.database().collection('appoint').where({
      patientAdmin:event.patientAdmin,
      appointDay:event.appointDay,
      isOver:event.isOver,
      isPassed:event.isPassed,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findDoctorShow"){
    return await cloud.database().collection('appoint').where({
      doctorId:event.doctorId,
      appointDay:event.appointDay,
      isOver:event.isOver,
      isPassed:event.isPassed,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findAShow"){
    return await cloud.database().collection('appoint').where({
      appointDay:event.appointDay,
      isOver:event.isOver,
      isPassed:event.isPassed,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findCShow"){
    return await cloud.database().collection('appoint').where({
      patientAdmin:event.patientAdmin,
      appointDay:event.appointDay,
      isOver:event.isOver,
      isPassed:event.isPassed,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  //查看医生的预约人数
  else if(event.action=="findNum"){
    return await cloud.database().collection('appoint').where({
      doctorId:event.doctorId,
      appointDay:event.appointDay,
      isPassed:event.isPassed,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findDoctor"){
    return await cloud.database().collection('appoint').where({
      doctorId:event.doctorId,
      appointDay:event.appointDay,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="findFlowId"){
    return await cloud.database().collection('appoint').where({
      flowId:event.flowId,
    }).get()
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="changePasedByFlowId"){
    return await cloud.database().collection('appoint').where({
      flowId:event.flowId,
    }).update({
      data:{
        isPassed:event.isPassed
      }
    })
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }
  else if(event.action=="changeOverByFlowId"){
    return await cloud.database().collection('appoint').where({
      flowId:event.flowId,
    }).update({
      data:{
        isOver:event.isOver
      }
    })
    .then(res => {
      return res
    }).catch(res => {
      return res
    })
  }


}

